# -*- coding: utf-8 -*-

import wsgiref.handlers
import sys
import os
import string
import codecs
import random
import re
import cgi
import datetime
import time
import math
import pickle
import logging
import Cookie
import urllib
import urllib2

DIR_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
LIB_PATH = os.path.join(DIR_PATH, "lib")
EXTRA_PATHS = []
for path in os.listdir(LIB_PATH):
    fullpath = os.path.join(LIB_PATH, path)
    if os.path.isdir(fullpath) and not path.startswith("."):
        EXTRA_PATHS.append(fullpath)
sys.path = sys.path + EXTRA_PATHS

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp

webapp.template.register_template_library('smart_if')
template.register_template_library('django.contrib.markup.templatetags.markup')

from math import sqrt
from google.appengine.api import mail
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import search
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from datetime import datetime
from google.appengine.api import images
from models import *
from utilities import *
from django.utils import simplejson
from translator import *
from paging import *

from google.appengine.ext.webapp import template

import datastore_cache
datastore_cache.DatastoreCachingShim.Install()

# Set to true if we want to have our webapp print stack traces, etc
_DEBUG = True

class BaseRequestHandler(webapp.RequestHandler):
  """Supplies a common template generation function.

  When you call generate(), we augment the template variables supplied with
  the variables present in this function.
  """
  def generate(self, template_name, template_values={}):

    values = {
      'request': self.request,
      'application_name': 'qstack',
    }
    values.update(template_values)
    directory = os.path.dirname(__file__)
    path = os.path.join(directory, os.path.join('templates', template_name))
    self.response.out.write(template.render(path, values, debug=_DEBUG))


class Index(BaseRequestHandler):
  def get(self):
    template_values = {
    }
    self.generate('index.html', template_values)

class GoogleVerification(BaseRequestHandler):
  def get(self, page = 1):
    template_values = {
    }
    self.generate('googleverification.html', template_values)


class EscudoPage(BaseRequestHandler):
  def get(self, slug):
    queryitems = Equipo.all()
    queryitems.filter(' slug = ', slug)
    items = queryitems.fetch(1)
    equipo = items[0]

    template_values = {
      'equipo': equipo,
    }
    self.generate('escudo.html', template_values)

class EscudosPage(BaseRequestHandler):
  def get(self, slug):
    queryitems = Country.all()
    queryitems.filter(' slug = ', slug)
    items = queryitems.fetch(1)
    
    if len(items) > 0:
      country = items[0]
      
      queryitems = Equipo.all()
      queryitems.filter(' country = ', country.name)
      queryitems.order('name')
      equipos = queryitems.fetch(500)
      
      template_values = {
        'country': country,
        'equipos': equipos,
      }
      self.generate('pais.html', template_values)
    else:
      confederation = slug
      
      queryitems = Country.all()
      queryitems.filter(' confederation = ', confederation)
      queryitems.order('name')
      countries = queryitems.fetch(500)
      
      template_values = {
        'confederation': confederation,
        'countries': countries,
      }
      self.generate('confederacion.html', template_values)

class EscudoPunto(BaseRequestHandler):
  def get(self, slug):
    queryitems = Equipo.all()
    queryitems.filter(' slug = ', slug)
    items = queryitems.fetch(1)
    equipo = items[0]

    template_values = {
      'equipo': equipo,
    }
    self.generate('punto.html', template_values)

class EscudoColorear(BaseRequestHandler):
  def get(self, slug):
    queryitems = Equipo.all()
    queryitems.filter(' slug = ', slug)
    items = queryitems.fetch(1)
    equipo = items[0]

    template_values = {
      'equipo': equipo,
    }
    self.generate('colorear.html', template_values)

class EscudosPunto(BaseRequestHandler):
  def get(self):
    template_values = {
    }
    self.generate('puntos.html', template_values)

class EscudosColorear(BaseRequestHandler):
  def get(self):
    template_values = {
    }
    self.generate('colorears.html', template_values)

class Privacidad(BaseRequestHandler):
  def get(self):

    template_values = {
    }
    self.generate('politica-privacidad.html', template_values)

class QuienesSomos(BaseRequestHandler):
  def get(self):

    template_values = {
    }
    self.generate('quienes-somos.html', template_values)

class Contacto(BaseRequestHandler):
  def get(self):

    template_values = {
    }
    self.generate('contacto.html', template_values)

class Enlaces(BaseRequestHandler):
  def get(self):
    queryitems = Link.all()
    queryitems.order('pubDate')
    links = queryitems.fetch(500)
    
    template_values = {
      'links': links,
    }
    self.generate('enlaces.html', template_values)

def main():
  urlmap = [
            ('/', Index),
            ('/google03e298e060fd8943.html', GoogleVerification),
            ('/escudo-(.*)/punto-cruz', EscudoPunto),
            ('/escudo-(.*)/para-colorear', EscudoColorear),
            ('/escudo-(.*)', EscudoPage),
            ('/escudos-futbol-punto-cruz', EscudosPunto),
            ('/escudos-futbol-para-colorear', EscudosColorear),
            ('/escudos-(.*)', EscudosPage),
            ('/privacidad', Privacidad),
            ('/contactar', Contacto),
            ('/acerca-de', QuienesSomos),
            ('/enlaces', Enlaces),
           ]

  application = webapp.WSGIApplication(urlmap, debug=_DEBUG)
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

